<%--
  Created by IntelliJ IDEA.
  User: ykj
  Date: 16/1/6
  Time: 下午2:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<style type="text/css">
    #data_menu {
    }
</style>
<script charset="utf-8">
    $(document).ready(function(){
//        $('#menu').accordion('getSelected').panel('collapse');
        $('#data_menu').tree({
            onSelect : function(node){
                addTab(node.text,node.url, node.table_id);
            }
        });

    });

    function addTab(title, url, table_id){
        var tab = $('#data_tabs');
        if (tab.tabs('exists', title)){
            tab.tabs('select', title);
        } else {
            var content = '<table id="grid_'+table_id+'"></table>';
            tab.tabs('add',{
                title:title,
                content:content,
                closable:true
            });
            console.log($('#grid_'+table_id));
            //ajax获取表字段
            $.get(table_id + '/getColumns', {'table_id': table_id},
                function (result) {
                    if (result instanceof Object) {
                        //获取对应表字段并构造datagrid可识别的columns对象
                        var columns = [[]];
                        for (var i = 0; i < result.fields.length; i++) {
                            var colOption = {};
                            colOption.field = result.fields[i];
                            colOption.title = result.titles[i];
                            colOption.width = 100;
                            columns[0].push(colOption);
                        }

                        $('#grid_' + table_id).datagrid({
                            url: url,
                            method: 'get',
                            columns: columns,
                            border: false,
                            singleSelect: true,
                            fit: true,
                            fitColumns: true,
                            pagination: true,
                            toolbar: [{
                                iconCls: 'icon-add',
                                text: '增加',
                                handler: function(){
                                    dialog_add(table_id);
                                }
                            },'-',{
                                iconCls: 'icon-edit',
                                text: '修改',
                                handler: function(){
                                    dialog_edit(table_id);
                                }
                            },'-',{
                                iconCls: 'icon-remove',
                                text: '删除',
                                handler: function(){
                                    confirm_delete(table_id);
                                }
                            },'-',{
                                iconCls: 'icon-help',
                                handler: function(){alert('help')}
                            }]
                        });

                        //这个位置可以加上对各个表的特殊处理
                        if(table_id=='user'){
                            //定义一个formater过滤数字
                            var numFormatter = function(val, row, index) {
                                return val.toFixed(2);
                            };
                            $('#grid_user').datagrid('getColumnOption', 'asset').formatter = numFormatter;
                            $('#grid_user').datagrid('getColumnOption', 'earnings').formatter = numFormatter;
                        }else if(table_id=='purchases'){
                            var stateFormatter = function(val, row, index) {
                                if(val=='801'){
                                    return '申请购买';
                                }else if(val=='802'){
                                    return '确认购买';
                                }else if(val=='803'){
                                    return '申请赎回';
                                }else if(val=='804'){
                                    return '确认赎回';
                                }else if(val=='805'){
                                    return '取消';
                                }
                            };
                            $('#grid_purchases').datagrid('getColumnOption', 'state').formatter = stateFormatter;
                        }
                    }
                }
            );
        }
    }
    function dialog_add(table_id) {
        $("<div></div>").dialog({
            id: table_id + "_add_dialog",
            href: table_id+'/addDialog',
            title: "增加",
            height: 400,
            width: 500,
            modal: true,
            onLoad:function() {

            },
            onClose: function () {
                $("#" + table_id + "_add_dialog").dialog('destroy');
            }
        });
    }
    function dialog_edit(table_id) {
        var row = $('#grid_'+table_id).datagrid('getSelected');
        if(row!=null){
            console.log(row.id);
            $("<div></div>").dialog({
                id: table_id + "_update_dialog",
                href: table_id+'/updateDialog?id='+row.id,
                title: "修改",
                height: 400,
                width: 500,
                modal: true,
                onLoad:function() {

                },
                onClose: function () {
                    $("#" + table_id + "_update_dialog").dialog('destroy');
                }
            });
        }else{
            $.messager.alert('提示','请先选择一条数据');
        }
    }
    function confirm_delete(table_id) {
        var row = $('#grid_'+table_id).datagrid('getSelected');
        if(row!=null){
            console.log(row.id);
            $.messager.confirm('提示','确认要删除这条记录吗?',function(r){
                if (r){
                    $.post(table_id+"/delete",{
                        id: row.id
                    },function(result){
                        if (result.success) {
                            $('#'+table_id+'_update_dialog').dialog('destroy');
                            $.messager.alert('提示','删除成功');
                            //刷新表格
                            $('#grid_'+table_id).datagrid('reload');
                        } else {
                            $.messager.alert('提示','删除失败,请换个姿势再来一遍');
                        }
                    });
                }
            });
        }else{
            $.messager.alert('提示','请先选择一条数据');
        }
    }

    var ykj = {};
    ykj.isJson = function(obj){
        var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
        return isjson;
    }
</script>
<div id="menu" class="easyui-accordion" data-options="fit:'true'">
    <div title="欢迎使用" data-options="iconCls:'icon-tip'" style="overflow:auto;padding:10px;">
        <h3 style="color:#0099FF;">欢迎使用华软理财产品系统</h3>
        <h3 style="color:#0099FF;">Welcome to use soft financial products system</h3>
        <p>华软理财产品系统,理财产品销售系统的极致,方便且美观的后台产品数据管理,让一切理财都变得轻轻松松.</p>
        <p>China soft products system, the perfection of financial product marketing system, convenient and beautiful and the background of product data management, make all money easily.</p>
    </div>
    <div title="数据管理" data-options="iconCls:'icon-reload',selected:true" style="padding:10px;">
        <ul id="data_menu" class="easyui-tree">
            <li data-options="iconCls:'icon-edit',url:'user/users',table_id:'user'"><span>用户</span></li>
            <li data-options="iconCls:'icon-edit',url:'type/types',table_id:'type'"><span>类型</span></li>
            <li data-options="iconCls:'icon-edit',url:'product/products',table_id:'product'"><span>理财产品</span></li>
            <li data-options="iconCls:'icon-edit',url:'purchases/purchases',table_id:'purchases'"><span>购买列表</span></li>
            <li data-options="iconCls:'icon-edit',url:'admin/admins',table_id:'admin'"><span>管理员</span></li>
        </ul>
    </div>
    <div title="Title3" data-options="iconCls:'icon-more'" style="padding:10px;">
        content3
    </div>
</div>
